package com.example.medicalapp.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.medicalapp.model.User;

public class PreferencesUtil {
    private static final String PREF_NAME = "MedicalAssistantPrefs";
    private static final String KEY_IS_LOGGED_IN = "is_logged_in";
    private static final String KEY_USER_ID = "user_id";
    private static final String KEY_USERNAME = "username";
    private static final String KEY_ROLE = "role";
    private static final String KEY_DOCTOR_ID = "doctor_id";

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    public PreferencesUtil(Context context) {
        sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    // 保存登录状态和用户信息
    public void saveLoginStatus(User user) {
        editor.putBoolean(KEY_IS_LOGGED_IN, true);
        editor.putInt(KEY_USER_ID, user.getId());
        editor.putString(KEY_USERNAME, user.getUsername());
        editor.putString(KEY_ROLE, user.getRole());
        editor.putString(KEY_DOCTOR_ID, user.getDoctorId());
        editor.commit();
    }

    // 清除登录状态
    public void clearLoginStatus() {
        editor.putBoolean(KEY_IS_LOGGED_IN, false);
        editor.remove(KEY_USER_ID);
        editor.remove(KEY_USERNAME);
        editor.remove(KEY_ROLE);
        editor.remove(KEY_DOCTOR_ID);
        editor.commit();
    }

    // 检查是否已登录
    public boolean isLoggedIn() {
        return sharedPreferences.getBoolean(KEY_IS_LOGGED_IN, false);
    }

    // 获取当前登录用户ID
    public int getUserId() {
        return sharedPreferences.getInt(KEY_USER_ID, -1);
    }

    // 获取当前登录用户名
    public String getUsername() {
        return sharedPreferences.getString(KEY_USERNAME, "");
    }

    // 获取当前用户角色
    public String getRole() {
        return sharedPreferences.getString(KEY_ROLE, "");
    }

    // 获取医生工号
    public String getDoctorId() {
        return sharedPreferences.getString(KEY_DOCTOR_ID, "");
    }
}

